package com.campsys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.QueryPageParam;
import com.campsys.common.Result;
import com.campsys.entity.User;
import com.campsys.service.UserService;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping({"/user"})
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> listAll(){
        return userService.listAll();
    }

    /*
    通过HashMap分页
     */
    @PostMapping("/listPage")
    public List<User> listP(@RequestBody HashMap userMap){
        Page<User> page = new Page<>(1,10);

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) userMap.get("loginName");
        lambdaQueryWrapper.like(User::getLoginName,name);

        Page<User> resultPage = userService.page(page, lambdaQueryWrapper);

        return resultPage.getRecords();
    }


    /*
    查询用户
    自定义分页
     */
    @PostMapping("/listPage1")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        Page<User> page =new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String name = (String) param.get("loginName");
        String userIdentity = (String) param.get("userIdentity");
        if (StringUtils.isNotBlank(name)){
            lambdaQueryWrapper.like(User::getLoginName,name);
        }
        if (StringUtils.isNotBlank(userIdentity)){
            lambdaQueryWrapper.eq(User::getUserIdentity,userIdentity);
        }

        IPage result =userService.page(page,lambdaQueryWrapper);

        return Result.suc(result.getTotal(),result.getRecords());
    }

    @PostMapping("/save")
    public Result save(@RequestBody User user){
        return userService.save(user)?Result.suc():Result.fail();
    }
    @PostMapping("/update")
    public Result update(@RequestBody User user){
        return userService.updateById(user)?Result.suc():Result.fail();
    }

    @GetMapping("/del")
    public Result delete(@RequestParam Integer id){
        return userService.removeById(id)?Result.suc():Result.fail();
    }

   //登录
   @PostMapping("/login")
   public Result login(@RequestBody User user){
        List list = userService.lambdaQuery()
                .eq(User::getLoginName,user.getLoginName())
                .eq(User::getPassword,user.getPassword()).list();
       return list.size()>0?Result.suc(list.get(0)):Result.fail();
   }

}
